﻿#ifndef AREAIDENTIFY_H
#define AREAIDENTIFY_H
#include "SerialPort.h"

class MainWindow;

class AreaIdentify : public QObject {
    Q_OBJECT
public:
	AreaIdentify(MainWindow& mw, std::shared_ptr<SerialPort> sp);
    void startQueryAreaResultTimer(uint32_t ms);


    void sendQueryTerminalNo();
    void sendStartAreaIdentify();
    void sendQueryTableFileAreaResult();
    void sendQueryRedundantAreaResult();

    void handleStartAreaIdentifyRsp(const QByteArray& relayMessage);
    void handleTableFileAreaResult(const QByteArray& relayMessage);
    void handleRedundantAreaResult(const QByteArray& relayMessage);
    void handleQueryTermialNoResult(const QByteArray& relayMessage);

public slots:
    // 周期查询台区识别结果
    void sendQueryAreaResultCycle();

private:
    // 查终端编号
    std::vector<int> queryTerminalNo = { 0x68, 0x0C, 0x00, 0x40, 0x03, 0x00, 0x03, 0x03, 0x00, 0xE8, 0x31, 0x16 };
    // 启动台区识别
    std::vector<int> startAreaIdentify = { 0x68, 0x0F, 0x00, 0x40, 0x04, 0x44, 0x80, 0x04, 0x02, 0xE8, 0x00, 0x00, 0x00, 0xF6, 0x16 };
    // 查询表档案的台区识别结果
    std::vector<int> queryTableFileAreaResult = { 0x68, 0x0F, 0x00, 0x40, 0x03, 0x0E, 0x0E, 0x03, 0x03, 0xE8, 0x00, 0x00, 0x04, 0x51, 0x16 };
    // 查询多余节点的台区识别结果
    std::vector<int> queryRedundantAreaResult = { 0x68, 0x0F, 0x00, 0x40, 0x03, 0x0E, 0x0F, 0x03, 0x03, 0xE8, 0x00, 0x00, 0x04, 0x52, 0x16 };

    MainWindow& mainWindow;

	std::shared_ptr<SerialPort> serialPort;

    // 查询到从节点表档案的数量
    int hasSearchedTableFileAreaCnt = 0;
    // 查询到从节点多余节点的数量
    int hasSearchedRedundantAreaCnt = 0;

    bool isStartedQueryAreaResultTimer = false;
    QTimer* queryAreaResultTimer;

    QString termialNo;
};

#endif